import 'dart:convert';

import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/dio/my_api.dart';

class TechnicianDetailsViewModel extends BaseViewModel {
  late TechnicianDetailModel detailModel;
  late List<TechnicianProjectDetailModel> projectDataList = [];
  @override
  requestData({Function(dynamic)? onSuccess, Function(dynamic)? onError, String? tId}) async {
    setState(ViewState.loading);

    // 去请求服务器数据.
    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.technicianInfo, method: DioHttpManager.post, data: {"t_id": tId ?? ""});
    if (resultMap["code"] == 0) {
      // 请求成功.
      detailModel = TechnicianDetailModel.fromMap(resultMap["data"]);
      List<dynamic> dataList = resultMap["project_data"];
      if (dataList.isNotEmpty) {
        for (var element in dataList) {
          projectDataList.add(TechnicianProjectDetailModel.fromMap(element));
        }
      }
    }

    // 更新页面.
    setState(ViewState.normal);
  }
}

class TechnicianDetailModel {
  final int? id;
  final int? tId;
  final int? mId;
  final String? headPortrait;
  final String? name;
  final int? sex;
  final int? age;
  final int? merchantsType;
  final String? certificate;
  final String? desc;
  final String? area;
  final String? imgs;
  final int? isFalse;
  final String? distance;
  final String? shopName;
  final String? earliestTimeStr;
  final int? isShield;
  final String? slotId;
  final int? isVip;
  final int? isOwn;
  final int? isShowPhone;
  final String? phone;
  final String? showPhone;
  final String? tip;
  final String? adCode;
  final int? interval;
  final String? imUser;

  TechnicianDetailModel({
    this.id,
    this.tId,
    this.mId,
    this.headPortrait,
    this.name,
    this.sex,
    this.age,
    this.merchantsType,
    this.certificate,
    this.desc,
    this.area,
    this.imgs,
    this.isFalse,
    this.distance,
    this.shopName,
    this.earliestTimeStr,
    this.isShield,
    this.slotId,
    this.isVip,
    this.isOwn,
    this.isShowPhone,
    this.phone,
    this.showPhone,
    this.tip,
    this.adCode,
    this.interval,
    this.imUser,
  });

  factory TechnicianDetailModel.fromJson(String str) => TechnicianDetailModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory TechnicianDetailModel.fromMap(Map<String, dynamic> json) => TechnicianDetailModel(
        id: json["id"],
        tId: json["t_id"],
        mId: json["m_id"],
        headPortrait: json["head_portrait"],
        name: json["name"],
        sex: json["sex"],
        age: json["age"],
        merchantsType: json["merchants_type"],
        certificate: json["certificate"],
        desc: json["desc"],
        area: json["area"],
        imgs: json["imgs"],
        isFalse: json["is_false"],
        distance: json["distance"],
        shopName: json["shop_name"],
        earliestTimeStr: json["earliest_time_str"],
        isShield: json["is_shield"],
        slotId: json["slot_id"],
        isVip: json["is_vip"],
        isOwn: json["is_own"],
        isShowPhone: json["is_show_phone"],
        phone: json["phone"],
        showPhone: json["show_phone"],
        tip: json["tip"],
        adCode: json["ad_code"],
        interval: json["interval"],
        imUser: json["im_user"],
      );

  Map<String, dynamic> toMap() => {
        "id": id,
        "t_id": tId,
        "m_id": mId,
        "head_portrait": headPortrait,
        "name": name,
        "sex": sex,
        "age": age,
        "merchants_type": merchantsType,
        "certificate": certificate,
        "desc": desc,
        "area": area,
        "imgs": imgs,
        "is_false": isFalse,
        "distance": distance,
        "shop_name": shopName,
        "earliest_time_str": earliestTimeStr,
        "is_shield": isShield,
        "slot_id": slotId,
        "is_vip": isVip,
        "is_own": isOwn,
        "is_show_phone": isShowPhone,
        "phone": phone,
        "show_phone": showPhone,
        "tip": tip,
        "ad_code": adCode,
        "interval": interval,
        "im_user": imUser,
      };
}

class TechnicianProjectDetailModel {
  final int? goodsId;
  final int? mId;
  final int? fCategoryId;
  final int? falseSales;
  final int? sales;
  final String? price;
  final String? coverImg;
  final String? title;
  final int? serviceDuration;
  final int? templateId;
  final String? salesStr;
  final List<GoodsImg>? goodsImgs;
  final int? jumpType;
  final String? serviceDurationStr;
  final String? goodsServiceFeeStr;
  final int? isDiscounts;
  final double? discountsRate;
  final String? goodsDiscountPrice;
  final String? titleDesc;

  TechnicianProjectDetailModel({
    this.goodsId,
    this.mId,
    this.fCategoryId,
    this.falseSales,
    this.sales,
    this.price,
    this.coverImg,
    this.title,
    this.serviceDuration,
    this.templateId,
    this.salesStr,
    this.goodsImgs,
    this.jumpType,
    this.serviceDurationStr,
    this.goodsServiceFeeStr,
    this.isDiscounts,
    this.discountsRate,
    this.goodsDiscountPrice,
    this.titleDesc,
  });

  factory TechnicianProjectDetailModel.fromJson(String str) => TechnicianProjectDetailModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory TechnicianProjectDetailModel.fromMap(Map<String, dynamic> json) => TechnicianProjectDetailModel(
        goodsId: json["goods_id"],
        mId: json["m_id"],
        fCategoryId: json["f_category_id"],
        falseSales: json["false_sales"],
        sales: json["sales"],
        price: json["price"],
        coverImg: json["cover_img"],
        title: json["title"],
        serviceDuration: json["service_duration"],
        templateId: json["template_id"],
        salesStr: json["sales_str"],
        goodsImgs:
            json["goods_imgs"] == null ? [] : List<GoodsImg>.from(json["goods_imgs"]!.map((x) => GoodsImg.fromMap(x))),
        jumpType: json["jump_type"],
        serviceDurationStr: json["service_duration_str"],
        goodsServiceFeeStr: json["goods_service_fee_str"],
        isDiscounts: json["isDiscounts"],
        discountsRate: json["discountsRate"]?.toDouble(),
        goodsDiscountPrice: json["goodsDiscountPrice"],
        titleDesc: json["title_desc"],
      );

  Map<String, dynamic> toMap() => {
        "goods_id": goodsId,
        "m_id": mId,
        "f_category_id": fCategoryId,
        "false_sales": falseSales,
        "sales": sales,
        "price": price,
        "cover_img": coverImg,
        "title": title,
        "service_duration": serviceDuration,
        "template_id": templateId,
        "sales_str": salesStr,
        "goods_imgs": goodsImgs == null ? [] : List<dynamic>.from(goodsImgs!.map((x) => x.toMap())),
        "jump_type": jumpType,
        "service_duration_str": serviceDurationStr,
        "goods_service_fee_str": goodsServiceFeeStr,
        "isDiscounts": isDiscounts,
        "discountsRate": discountsRate,
        "goodsDiscountPrice": goodsDiscountPrice,
        "title_desc": titleDesc,
      };
}

class GoodsImg {
  final int? goodsImgId;
  final String? img;
  final int? sort;

  GoodsImg({
    this.goodsImgId,
    this.img,
    this.sort,
  });

  factory GoodsImg.fromJson(String str) => GoodsImg.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory GoodsImg.fromMap(Map<String, dynamic> json) => GoodsImg(
        goodsImgId: json["goods_img_id"],
        img: json["img"],
        sort: json["sort"],
      );

  Map<String, dynamic> toMap() => {
        "goods_img_id": goodsImgId,
        "img": img,
        "sort": sort,
      };
}
